package cn.cs.service;

import cn.cs.common.Constants;
import cn.cs.common.DateUtil;
import cn.cs.common.EmptyUtils;
import cn.cs.common.Page;
import cn.cs.mapper.CsProductMapper;
import cn.cs.mapper.CsShopOrderProRelMapper;
import cn.cs.pojo.CsImage;
import cn.cs.pojo.CsProduct;
import cn.cs.pojo.CsShopOrderProRel;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * yuexl
 */
@RestController
public class RestCsProductService {

    @Resource
    private CsProductMapper csProductMapper;
    @Resource
    private CsShopOrderProRelMapper csShopOrderProRelMapper;
    @Resource
    private DateUtil dateUtil;
    @RequestMapping(value = "/getCsProductById", method = RequestMethod.POST)
    public CsProduct getCsProductById(@RequestParam("id") Long id) throws Exception {
        return csProductMapper.getCsProductById(id);
    }

    @RequestMapping(value = "/getCsProductListByMap", method = RequestMethod.POST)
    public List<CsProduct> getCsProductListByMap(@RequestBody Map<String, Object> param) throws Exception {
        return csProductMapper.getCsProductListByMap(param);
    }

    @RequestMapping(value = "/getHotCsProductList", method = RequestMethod.POST)
    public List<CsProduct> getHotCsProductList(@RequestBody Map<String, Object> param) throws Exception {
        return csProductMapper.getHotCsProductList(param);
    }

    @RequestMapping(value = "/getCsProductCountByMap", method = RequestMethod.POST)
    public Integer getCsProductCountByMap(@RequestParam Map<String, Object> param) throws Exception {
        return csProductMapper.getCsProductCountByMap(param);
    }

    @RequestMapping(value = "/qdtxAddDmItem", method = RequestMethod.POST)
    public Integer addCsProduct(@RequestBody CsProduct csProduct) throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        csProduct.setCreateTime(sdf.format(new Date()));
        return csProductMapper.insertCsProduct(csProduct);
    }

    @RequestMapping(value = "/qdtxModifyDmItem", method = RequestMethod.POST)
    public Integer modifyCsProduct(@RequestBody CsProduct csProduct) throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        csProduct.setUpdateTime(dateUtil.format(new Date()));
        return csProductMapper.updateCsProduct(csProduct);
    }


    @RequestMapping(value = "/getCsProductListByMapForEs", method = RequestMethod.POST)
    public List<CsProduct> getCsProductListByMapForEs(@RequestParam Map<String, Object> param) throws Exception {
        List<CsProduct> list = csProductMapper.getHotCsProductListForEs(param);
        return list;
    }

    @RequestMapping(value = "/queryCsProductPage", method = RequestMethod.POST)
    public Page<CsProduct> queryCsProducttPage(@RequestParam Map<String,Object> param)throws Exception{
        Integer total = csProductMapper.getCsProductCountByMap(param);
        Object pageNoObj=param.get("currentPage");
        Object pageSizeObj=param.get("pageSize");
        Integer pageNo=EmptyUtils.isEmpty(pageNoObj) ? Constants.DEFAULT_PAGE_NO : Integer.parseInt(pageNoObj.toString());
        Integer pageSize=EmptyUtils.isEmpty(pageSizeObj) ? Constants.DEFAULT_PAGE_SIZE : Integer.parseInt(pageSizeObj.toString());
        Page page = new Page(pageNo, pageSize, total);
        param.put("beginPos", page.getBeginPos());
        param.put("pageSize", page.getPageSize());
        List<CsProduct> dyTestProfessionTypeList = csProductMapper.getCsProductListByMap(param);
        page.setRows(dyTestProfessionTypeList);
        return page;
    }
    /**
     * 获取产品的销量
     * */
    @RequestMapping(value = "/getProductSaleCountById",method = RequestMethod.POST)
    public int getProductSaleCountById(@RequestParam("id") String id)throws Exception{
        List<CsShopOrderProRel> csShopOrderProRels = new ArrayList<CsShopOrderProRel>();
        Map<String,Object> param = new HashMap<String, Object>();
        param.put("cellProductId",id);
        csShopOrderProRels = csShopOrderProRelMapper.getShopOrderProRelListByMap(param);
        int count=0;
        for(CsShopOrderProRel csShopOrderProRel:csShopOrderProRels){
            count += Integer.parseInt(csShopOrderProRel.getProCount());
        }
        return count;
    }
}
